<template>
  <div class="app">
    <div class="login_container">
      <div class="login_box">
      <div class="avatar_box">
        <img
          src="http://destiny001.gitee.io/admin-dist/img/logo.b5a855ee.png"
          alt="黑马"
        />
      </div>
      <el-form  status-icon ref="form" :model="form" :rules="rules" class="login_form">
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            inline-message="true"
            type="password"
            autocomplete="off"
            prefix-icon="el-icon-lock"
            v-model="form.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-row class="login_form_anniu">
            <el-button  type="primary" @click="onSubmit(form)">登录</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </div>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },

      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3-10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3-10个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async onSubmit (form) {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false
        }
      })
      try {
        const res = await login(this.form)
        // console.log(res)
        if (res.data.meta.status !== 200) {
          return this.$message.error('错了哦，再看看吧')
        }
        this.$store.commit('initUser', res.data.data)
        // console.log(res.data.meta)
        this.$router.push('/welcome')
        this.$message({
          message: '登录成功',
          type: 'success'
        })
      } catch (err) {
        console.log(err)
      }
    },
    reset () {
      this.form.username = 'admin'
      this.form.password = '123456'
    }
  }
}
</script>

<style scoped lang="less">
.login_container{
  background-color: #2B4B6B;
  height:100%;
  .login_box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      background-color: #fff;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      height: 186px;
      padding: 0 20px;
      box-sizing: border-box;
      .login_form_anniu {
        margin-left: 260px;
      }
    }
  }
}

</style>
